// pages/doorLock/doorLock.js
import api from "../../api/index";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    shopID: null,
    shopData: {
      name: "shop_id",
      list: [],
    },

    list: [],
    isMore: true,
    isLoading: false,
    isQrCode: false,
    isOpen: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      list: [],
      shopID: wx.getStorageSync("shopID"),
    });
    this.getShopList();
    this.getDataList();
  },

  getShopValue(e) {
    this.setData({
      list: [],
      shopID: e.detail.value,
    });
    this.getDataList();
  },
  getShopList() {
    api.apiStoreShopList().then((res) => {
      this.setData({
        "shopData.list": res.data.list,
      });
    });
  },
  async getDataList() {
    this.setData({
      isLoading: true,
    });
    await this.getList();
    await this.getVgQrcode();
    this.setData({
      isLoading: false,
    });
  },
  getList() {
    return new Promise((resolve, reject) => {
      let { list, shopID } = this.data;
      api
        .apiStoreDeviceList({
          shop_id: shopID,
        })
        .then((res) => {
          if (shopID !== this.data.shopID) return;
          let data = res.data;
          list.push(...data.list);
          this.setData({ list });
          resolve();
        });
    });
  },
  async refreshQrCode() {
    this.setData({
      isQrCode: true,
    });
    wx.showLoading({
      title: "加载中",
      mask: true,
    });
    await this.getVgQrcode();
    wx.hideLoading();
  },
  getVgQrcode() {
    return new Promise((resolve, reject) => {
      let { shopID } = this.data;
      api
        .apiStoreVgQrcode({
          shop_id: shopID,
        })
        .then((res) => {
          if (shopID !== this.data.shopID) return;
          let vgQrcode = res.data;
          this.setData({ vgQrcode });
          resolve();
        });
    });
  },
  openDoor(e) {
    if (this.data.isOpen) return;
    this.setData({ isOpen: true });
    wx.showLoading({
      title: "开门中...",
      mask: true,
    });
    api
      .apiStoreOpenDevice({
        device_sn: e.currentTarget.dataset.sn,
      })
      .then((res) => {
        if (res.code === 601) {
          wx.showModal({
            title: "开门成功",
            content: "当前门已开启，请确认！",
            confirmText: "确认",
            confirmColor: "#73F0EA",
            showCancel: false,
          });
        } else {
          wx.showToast({
            title: res.msg,
            icon: "none",
          });
        }
      })
      .finally((_) => {
        this.setData({ isOpen: false });
        wx.hideLoading();
      });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
});
